import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
  history: createWebHistory(),
  routes: [
    //登录
    {path:"/login",name:"login",component:()=>import("@/views/login/index.vue")},
    //首页
    {path:"/",name:"home",
      redirect:"/home",
      component:()=>import("@/layout/MyLayout.vue"),
      meta: { requiresAuth: true ,title:"系统首页", iconName:"House"},
      children:[
        {path:"/home",component:()=>import("@/views/home/index.vue")}
      ]
    },
    //轮播
    {path:"/banner",name:"banner",
      redirect:'/banner/list',
      component:()=>import("@/layout/MyLayout.vue"),
      meta: { requiresAuth: true ,title:"轮播图管理", iconName:"Picture"},
      children:[
        {path:"/banner/list",
          redirect:"/banner/list/home",
          meta: { requiresAuth: true ,title:"轮播图列表", iconName:"Picture"},
          component:()=>import("@/views/banner/index.vue"),
          children:[
            {path:"/banner/list/home", meta: { requiresAuth: true ,title:"首页轮播图列表", iconName:"Picture"},component:()=>import("@/views/banner/components/BannerHome.vue")},
            {path:"/banner/list/kind", meta: { requiresAuth: true ,title:"分类轮播图列表", iconName:"Picture"},component:()=>import("@/views/banner/components/Bannerkind.vue")},
          ]
        }
      ]
    },
    //产品
    {path:"/pro",name:"pro",
      redirect:'/pro/list',
      component: () => import("@/layout/MyLayout.vue"),
      meta: { requiresAuth: true ,title:"产品管理", iconName:"Fries"},
      children:[
        {path:"/pro/list",name:"proList",meta: { requiresAuth: true ,title:"产品列表", iconName:"Fries"},component:()=>import("@/views/pro/ProList.vue")},
        {path:"/pro/kind",name:"proKind",meta: { requiresAuth: true ,title:"筛选列表", iconName:"Fries"},component:()=>import("@/views/pro/ProKind.vue")}
      ]
    },
    //账户管理
    {path:"/account",name:"account",
      redirect:'/account/adminlist',
      component: () => import("@/layout/MyLayout.vue"),
      meta: { requiresAuth: true ,title:"账户管理",iconName:"User"},
      children:[
        {path:"/account/adminlist",name:"adminlist",meta: { requiresAuth: true ,title:"管理员列表",iconName:"User"},component:()=>import("@/views/account/AdminList.vue")},
        {path:"/account/userlist",name:"userlist",meta: { requiresAuth: true ,title:"用户列表",iconName:"User"},component:()=>import("@/views/account/UserList.vue")}
      ]
    },
    //导入导出
    {path:"/importAndexport",name:"importAndexport",
      redirect:'/importAndexport/export',
      component: () => import("@/layout/MyLayout.vue"),
      meta: { requiresAuth: true ,title:"导入及导出",iconName:"Switch"},
      children:[
        {path:"/importAndexport/export",name:"export",meta: { requiresAuth: true ,title:"导出",iconName:"Switch"},component:()=>import("@/views/importAndexport/Export.vue")},
        {path:"/importAndexport/import",name:"import",meta: { requiresAuth: true ,title:"导入",iconName:"Switch"},component:()=>import("@/views/importAndexport/Import.vue")}
      ]
    },

  ],
  
})
// 导入useUserStore但不在此处直接使用，避免在Vue应用实例创建前访问store
import { useUserStore } from '@/stores/useUser';

// 路由守卫函数，在main.ts中正确初始化后使用
router.beforeEach((to, from) => {
  // 检查路由或其子路由是否需要认证
  const requiresAuth = to.matched.some(record => record.meta.requiresAuth);
  
  if (requiresAuth) {
    // 确保在Vue应用实例创建后再获取store实例
    const userStore = useUserStore();
    
    if (!userStore.isLogin) {
      // 此路由需要授权，但用户未登录，重定向到登录页面
      return {
        path: '/login',
        // 保存当前路径，登录成功后可以跳转回来
        query: { redirect: to.fullPath },
      }
    }
  }
})
export default router
